package com.lcc.control;

import com.google.gson.Gson;
import com.lcc.pojo.ShoppingCart;
import com.lcc.service.BookService;
import com.lcc.utils.BookStoreWebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author 廖聪聪
 * @version 1.0
 * 创建时间 2020/7/25 16:03
 */
@Controller
@RequestMapping("/cart")
public class ItemCartContrl {
	@Autowired
	private BookService bookService;
	@RequestMapping(value = "tocar",method = RequestMethod.GET)
	public String gogouche(){
		return "cart";
	}
	/**
	 * 购物车添加商品
	 */
	@RequestMapping(value = "gocart",method = RequestMethod.GET)
	public String gocart(HttpServletRequest request){
        //1.获取商品的id
		String isStr = request.getParameter("bookid");
		int id= -1;
		boolean flag = false;
		try {
			id = Integer.parseInt(isStr);
		}catch (Exception e){
			e.printStackTrace();
		}
		if (id>0){
			//2。获取购物车对象
			ShoppingCart sc = BookStoreWebUtils.getShoppingCart(request);
			//3. 调用 BookService 的 addToCart() 方法把商品放到购物车中
           flag= bookService.addToCart(id,sc);
		}
		return "cart";
	}

	/**
	 * 清除购物车
	 */
	@RequestMapping(value = "clearCart",method = RequestMethod.GET)
	public String clearCart(HttpServletRequest request){
		ShoppingCart shoppingCart = BookStoreWebUtils.getShoppingCart(request);
		bookService.clearShoppingCart(shoppingCart);
		return "cart";
	}

	/**
	 * 根据id清除指定购物项
	 */
	@RequestMapping(value = "remove",method = RequestMethod.GET)
	public String removeByid(Integer bookid,HttpServletRequest request){
		ShoppingCart sc= BookStoreWebUtils.getShoppingCart(request);
		bookService.removeItemFromShoppingCart(sc,bookid);
		return "cart";
	}
	/**
	 * 修改指定的购买数量
	 */
	@RequestMapping(value = "updateqnum",method = RequestMethod.POST)
	public void updatequantity(HttpServletRequest request, HttpServletResponse response) throws IOException {

		//1. 在 updateItemQuantity 方法中, 获取 quanity, id, 再获取购物车对象, 调用 service 的方法做修改
		String idStr = request.getParameter("id");
		String quantityStr = request.getParameter("quantity");

		ShoppingCart sc= BookStoreWebUtils.getShoppingCart(request);

		int id = -1;
		int quantity = -1;
		try {
			id = Integer.parseInt(idStr);
			quantity = Integer.parseInt(quantityStr);
		}catch (Exception e){
			e.printStackTrace();
		}

		if (id>0 && quantity>0){
			bookService.updateItemQuantity(sc,id,quantity);
			//数据使用json传回前端页面
			Map<String ,Object> result = new HashMap<String, Object>();
			result.put("bookNumber",sc.getBookNumber());
			result.put("totalMoney",sc.getTotalMoney());
			//System.out.println(result);
			Gson gson  = new Gson();
			//System.out.println(8888);
			String jsonStr = gson.toJson(result);
			System.out.println("打印json数据："+jsonStr);
			response.setContentType("text/javascript");
			response.getWriter().print(jsonStr);
		}
	}

}
